//index.js
//获取应用实例
const app = getApp()
var util = require("../../utils/libs.js")
var api = require("../../utils/api.js")
var date = require("../../utils/date.js")
var WxParse = require('../../wxParse/wxParse.js');

Page({
  data: {
    tabs: [{ fun: "indexTo", menus: '首页', url: '../../imgs/index/index.png', icon: 'indexI', active: 'active' }, { fun: "publishTo", icon: "centerI", menus: '发布', url: '../../imgs/index/center_c.png', active: '' }, { fun: "adminTo", menus: '我的', url: '../../imgs/index/admin_c.png', icon: 'lastI', active: '' }],
    banners: [], 
    skillS:[],
    indicatorDots: false,
    imgActive:'item-img',
    bgImg:'item-imgs',
    autoplay: true,
    active:0,
    textColor:'text',
    fontColor:'text',
    Color:'text',
    circular:true,
    interval: 3000,
    duration: 1000,
    image: '../../imgs/index/publish.png',
    avatar:"",
    gander:"",
    province:"",
    city:"",
    nickname:"",
    openid:"",
    uid:null,
    recommends:[],
    helps:[],
    skill:[],
    topics:[],
    publichS:[], globalData:{  
      appid:'wxb78b3211945cae67',//appid需自己提供，此处的appid我随机编写  
      secret:'ad9e02f1ea741473fa5615787c0e9d7e',//secret需自己提供，此处的secret我随机编写  
    },  

  },
  select:function(){
    wx.navigateTo({
      url: '../59select/59select',
    })
  },
  edit:function(){
    wx.navigateTo({
      url: '../subsample/subsample',
    })
  },
  go () {
    wx.navigateTo({
      url: '/pages/skill/skill',
    })
  },
  bannerTo(event){
    let url = event.currentTarget.dataset.url;
    if(url==""||url==null) return ;
    wx.navigateTo({
      url:'../../'+url,
    })
  },
  pub(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "/pages/listdetais/listdetais?id=" + id
    })
  },
  skill(event){
    let id = event.currentTarget.dataset.id;
    if (id == "" || id == null) return;
    wx.navigateTo({
      url: '../article/article?id='+id,
    })
  },
  skillsTo(event) {
    let id = event.currentTarget.dataset.id;
    if (id == "" || id == null) return;
    wx.navigateTo({
      url: '../skillarticle/skillarticle?id=' + id,
    })
  },
  skillTo(event) {
    let id = event.currentTarget.dataset.id;
    if (id == "" || id == null) return;
    wx.navigateTo({
      url: '../article/article?id=' + id +'&param=rerecommend',
    })
  },
  goSearch(event){
    let keyword = event.detail.value;
    if(keyword==''){
      wx.showToast({
        title: '请输入关键字',
        icon: 'none',
        duration: 2000
      })
      return ;
    }
    wx.navigateTo({
      url: '../search/search?key='+keyword,
    })
  },
  getContent(s) {
    //获取文章内容节点
    var nodes = wx.getStorageSync('htmldata');
    var arr = nodes.nodes;
    for (var i = 0; i < arr.length; i++) {
      var arr2 = arr[i].nodes;
      for (var j = 0; j < arr2.length; j++) {
        var arr3 = arr2[j].nodes
        if (arr3 != undefined) {
          for (var l = 0; l < arr3.length; l++) {
            var arr4 = arr3[l].nodes;
            if (arr4 != undefined) {
              for (var m = 0; m < arr4.length; m++) {
                s += arr4[m].text;
              }
            }
          }
        }
      }
    }
    return s
  },
  wrtingTo(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "/pages/writing/writing?id=" + id
    })
  },
  reviewTo(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "/pages/assist/assist?id=" + id
    })
  },
  onLoad(){

    let that = this
    util.request(api.banner, {}, "POST")
      .then(function (res) {
        if (res.error_code===0){
          that.setData({
            banners:res.data.list
          })
        }
      })
    util.request(api.Skill, { order:'desc'},"POST")
    .then(function(res){
      if (res.error_code === 0) {
        var arr = res.data.list;
        arr.forEach(e => {
          e.create_time = date.data(e.create_time)
          e.description = e.description.substring(0, 120) + '……'
        });
      // for(var i=0;i<res.data.list.length;i++){
      //   WxParse.wxParse('content', 'html', res.data.list[i].content, that, 0);
      //   // 获取文章内容
      //   var s = '';
      //   var s2 = that.getContent(s);
      //   var str = s2.substring(0, 30) + '……'
      //   res.data.list[i].content= str;
      // }
      that.setData({
        skillS: arr,
      })
      }
    })
    let uid = wx.getStorageSync("uid");
    util.request(api.common, { page:1, key: '', limit:9999, uid:'' }, 
    "GET").then(function (res) {
      if (res.error_code === 0) {
        var arr= res.data.build.list;
        var arr2 = res.data.help.list;
        var arr3 = res.data.recommend.list;
        var arr4 = res.data.skill.list;
        var arr5 =res.data.topic.list;
         arr.forEach(e => {
            e.create_time = date.data(e.create_time)
            e.content = e.content.substring(0,40)+'……'
            if (e.now_step==0){
              e.now_step = 1
            }
          })
          arr2.forEach(e => {
            e.create_time = date.data(e.create_time)
            e.content = e.content.substring(0, 40) + '……'
            if (e.now_step == 0) {
              e.now_step = 1
            } 
            if(e.images==null){
              e.images = '../../imgs/help.jpg'
            }
          })
          arr3.forEach(e => {
            e.create_time = date.data(e.create_time)
            e.content = e.content.substring(0, 40) + '……'
            if (e.now_step == 0) {
              e.now_step = 1
            }
            if (e.images == null) {
              e.images = '../../imgs/recommend.jpg'
            }
          })
          arr4.forEach(e => {
            e.create_time = date.data(e.create_time)
            e.content = e.content.substring(0, 40) + '……'
            if (e.now_step == 0) {
              e.now_step = 1
            }
            if (e.image == null||e.image=='') {
              e.image= '../../imgs/skill.jpg'
            }
          });
          arr5.forEach(e => {
            e.create_time = date.data(e.create_time)
            e.description = e.description.substring(0, 40) + '……'
            if (e.now_step == 0) {
              e.now_step = 1
            }
            if (e.images == null|| e.iamges=='') {
              e.images = '../../imgs/topic.jpg'
            }
          });
          that.setData({
            publishS: arr,
            recommends: arr3,
            helps: arr2,
            skill: arr4,
            topics: arr5
          })
        }
    })
  }
})